View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact tma@netspace.net.au. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2000-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: FormatConverter.java,v 1.2 2004/02/02 03:49:55 tanderson Exp $ 44 */ 45 46 package org.exolab.jmscts.jms.message; 47 48 import javax.jms.MessageFormatException; 49 50 51 /*** 52 * A simple format converter to help convert an Object type as per the 53 * table listed below. 54 * 55 * <P>A value written as the row type can be read as the column type. 56 * 57 * <PRE> 58 * | | boolean byte short char int long float double String byte[] 59 * |---------------------------------------------------------------------- 60 * |boolean | X X 61 * |byte | X X X X X 62 * |short | X X X X 63 * |char | X X 64 * |int | X X X 65 * |long | X X 66 * |float | X X X 67 * |double | X X 68 * |String | X X X X X X X X 69 * |byte[] | X 70 * |---------------------------------------------------------------------- 71 * </PRE> 72 * 73 * <P>Attempting to read a null value as a Java primitive type must be treated 74 * as calling the primitive's corresponding <code>valueOf(String)</code> 75 * conversion method with a null value. Since char does not support a String 76 * conversion, attempting to read a null value as a char must throw 77 * NullPointerException. 78 * 79 * @version $Revision: 1.2 $ $Date: 2004/02/02 03:49:55 $ 80 * @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a> 81 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 82 */ 83 final class FormatConverter { 84 85 /*** 86 * Prevent construction of utility class 87 */ 88 private FormatConverter() { 89 } 90 91 /*** 92 * Convert value to boolean 93 * 94 * @param value the object to convert from 95 * @return the converted boolean 96 * @throws MessageFormatException if the conversion is invalid 97 */ 98 public static boolean getBoolean(Object value) 99 throws MessageFormatException { 100 boolean result = false; 101 102 if (value instanceof Boolean) { 103 result = ((Boolean) value).booleanValue(); 104 } else if (value instanceof String) { 105 result = Boolean.valueOf((String) value).booleanValue(); 106 } else if (value == null) { 107 result = Boolean.valueOf((String) value).booleanValue(); 108 } else { 109 raise(value, boolean.class); 110 } 111 return result; 112 } 113 114 /*** 115 * Convert value to byte 116 * 117 * @param value the object to convert from 118 * @return the converted byte 119 * @throws MessageFormatException if the conversion is invalid 120 * @throws NumberFormatException if value is a String and the conversion 121 * is invalid 122 */ 123 public static byte getByte(Object value) throws MessageFormatException { 124 byte result = 0; 125 126 if (value instanceof Byte) { 127 result = ((Byte) value).byteValue(); 128 } else if (value instanceof String) { 129 result = Byte.parseByte((String) value); 130 } else if (value == null) { 131 result = Byte.valueOf((String) value).byteValue(); 132 } else { 133 raise(value, byte.class); 134 } 135 return result; 136 } 137 138 139 /*** 140 * Convert value to short 141 * 142 * @param value the object to convert from 143 * @return the converted short 144 * @throws MessageFormatException if the conversion is invalid 145 * @throws NumberFormatException if value is a String and the conversion 146 * is invalid 147 */ 148 public static short getShort(Object value) throws MessageFormatException { 149 short result = 0; 150 151 if (value instanceof Short) { 152 result = ((Short) value).shortValue(); 153 } else if (value instanceof Byte) { 154 result = ((Byte) value).shortValue(); 155 } else if (value instanceof String) { 156 result = Short.parseShort((String) value); 157 } else if (value == null) { 158 result = Short.valueOf((String) value).shortValue(); 159 } else { 160 raise(value, short.class); 161 } 162 return result; 163 } 164 165 /*** 166 * Convert value to char 167 * 168 * @param value the object to convert from 169 * @return the converted char 170 * @throws MessageFormatException if the conversion is invalid 171 * @throws NullPointerException if value is null 172 */ 173 public static char getChar(Object value) throws MessageFormatException { 174 char result = '\0'; 175 if (value instanceof Character) { 176 result = ((Character) value).charValue(); 177 } else if (value == null) { 178 throw new NullPointerException( 179 "Cannot convert null value to char"); 180 } else { 181 raise(value, char.class); 182 } 183 return result; 184 } 185 186 /*** 187 * Convert value to int 188 * 189 * @param value the object to convert from 190 * @return the converted int 191 * @throws MessageFormatException if the conversion is invalid 192 * @throws NumberFormatException if value is a String and the conversion 193 * is invalid 194 */ 195 public static int getInt(Object value) throws MessageFormatException { 196 int result = 0; 197 198 if (value instanceof Integer) { 199 result = ((Integer) value).intValue(); 200 } else if (value instanceof Short) { 201 result = ((Short) value).intValue(); 202 } else if (value instanceof Byte) { 203 result = ((Byte) value).intValue(); 204 } else if (value instanceof String) { 205 result = Integer.parseInt((String) value); 206 } else if (value == null) { 207 result = Integer.valueOf((String) value).intValue(); 208 } else { 209 raise(value, int.class); 210 } 211 return result; 212 } 213 214 /*** 215 * Convert value to long 216 * 217 * @param value the object to convert from 218 * @return the converted long 219 * @throws MessageFormatException if the conversion is invalid 220 * @throws NumberFormatException if value is a String and the conversion 221 * is invalid 222 */ 223 public static long getLong(Object value) throws MessageFormatException { 224 long result = 0; 225 226 if (value instanceof Long) { 227 result = ((Long) value).longValue(); 228 } else if (value instanceof Integer) { 229 result = ((Integer) value).longValue(); 230 } else if (value instanceof Short) { 231 result = ((Short) value).longValue(); 232 } else if (value instanceof Byte) { 233 result = ((Byte) value).longValue(); 234 } else if (value instanceof String) { 235 result = Long.parseLong((String) value); 236 } else if (value == null) { 237 result = Long.valueOf((String) value).longValue(); 238 } else { 239 raise(value, long.class); 240 } 241 return result; 242 } 243 244 /*** 245 * Convert value to float 246 * 247 * @param value the object to convert from 248 * @return the converted float 249 * @throws MessageFormatException if the conversion is invalid 250 * @throws NumberFormatException if value is a String and the conversion 251 * is invalid 252 */ 253 public static float getFloat(Object value) throws MessageFormatException { 254 float result = 0; 255 256 if (value instanceof Float) { 257 result = ((Float) value).floatValue(); 258 } else if (value instanceof String) { 259 result = Float.parseFloat((String) value); 260 } else if (value == null) { 261 result = Float.valueOf((String) value).floatValue(); 262 } else { 263 raise(value, float.class); 264 } 265 return result; 266 } 267 268 /*** 269 * Convert value to double 270 * 271 * @param value the object to convert from 272 * @return the converted double 273 * @throws MessageFormatException if the conversion is invalid 274 * @throws NumberFormatException if value is a String and the conversion 275 * is invalid 276 */ 277 public static double getDouble(Object value) 278 throws MessageFormatException { 279 double result = 0; 280 281 if (value instanceof Double) { 282 result = ((Double) value).doubleValue(); 283 } else if (value instanceof Float) { 284 result = ((Float) value).doubleValue(); 285 } else if (value instanceof String) { 286 result = Double.parseDouble((String) value); 287 } else if (value == null) { 288 result = Double.valueOf((String) value).doubleValue(); 289 } else { 290 raise(value, double.class); 291 } 292 return result; 293 } 294 295 /*** 296 * Convert value to String 297 * 298 * @param value the object to convert from 299 * @return the converted String 300 * @throws MessageFormatException if the conversion is invalid 301 */ 302 public static String getString(Object value) 303 throws MessageFormatException { 304 if (value instanceof byte[]) { 305 raise(value, String.class); 306 } 307 return (value == null) ? null : String.valueOf(value); 308 } 309 310 /*** 311 * Convert value to byte[] 312 * 313 * @param value the object to convert from. This must be a byte array, or 314 * null 315 * @return a copy of the supplied array, or null 316 * @throws MessageFormatException if value is not a byte array or is not 317 * null 318 */ 319 public static byte[] getBytes(Object value) 320 throws MessageFormatException { 321 byte[] result = null; 322 323 if (value instanceof byte[]) { 324 byte[] bytes = (byte[]) value; 325 result = new byte[bytes.length]; 326 System.arraycopy(bytes, 0, result, 0, bytes.length); 327 } else if (value != null) { 328 raise(value, byte[].class); 329 } 330 return result; 331 } 332 333 /*** 334 * Helper to raise a MessageFormatException when a conversion cannot be 335 * performed 336 * 337 * @param value the value that cannot be converted 338 * @param type the type that the value cannot be converted to 339 * @throws MessageFormatException when invoked 340 */ 341 private static void raise(Object value, Class type) 342 throws MessageFormatException { 343 344 throw new MessageFormatException( 345 "Cannot convert values of type " + value.getClass().getName() 346 + " to " + type.getName()); 347 } 348 349 }

This page was automatically generated by Maven